//
//  XQPhotoView.m
//  first
//
//  Created by 毕小强 on 17/1/12.
//  Copyright © 2017年 毕小强. All rights reserved.
//

#import "XQPhotoView.h"
@interface XQPhotoView()
{
    UIImageView *_imageView;
    CGRect _vFrame;
}
@end
@implementation XQPhotoView
-(instancetype)initWithImage:(UIImage *)image{
    if (self = [super init]) {
        self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,  [UIScreen mainScreen].bounds.size.height);
        self.backgroundColor = [UIColor blackColor];
        
        CGFloat height = [UIScreen mainScreen].bounds.size.width/image.size.width*image.size.height;
        _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, ([UIScreen mainScreen].bounds.size.height-height)/2.0, [UIScreen mainScreen].bounds.size.width, height)];
        _imageView.userInteractionEnabled = YES;
        _imageView.image = image;
        [self addSubview:_imageView];
        
        _vFrame = _imageView.frame;
        self.userInteractionEnabled = YES;
        //捏合
        UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGestureDetected:)];
        [_imageView addGestureRecognizer:pinchGesture];
        
        //旋转
        //        UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGestureDetected:)];
        //        [self addGestureRecognizer:rotationGesture];
        
        //移动
        UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureDelegated:)];
        [_imageView addGestureRecognizer:panGesture];
        
        //单击
        UITapGestureRecognizer *tapOneGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapOneGestureDelegated:)];
        [tapOneGesture setNumberOfTapsRequired:1];
        tapOneGesture.numberOfTouchesRequired = 1;
        [_imageView addGestureRecognizer:tapOneGesture];
        
        //双击
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureDelegated:)];
        [tapGesture setNumberOfTapsRequired:2];
        tapGesture.numberOfTouchesRequired = 1;
        [_imageView addGestureRecognizer:tapGesture];
        
        [tapOneGesture requireGestureRecognizerToFail:tapGesture];
    }
    return self;
}
-(void)pinchGestureDetected:(UIPinchGestureRecognizer*)recognizer{
    //捏合
    UIGestureRecognizerState state = [recognizer state];
    if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged) {
        CGFloat scale = [recognizer scale];
        [recognizer.view setTransform:CGAffineTransformScale(recognizer.view.transform, scale, scale)];
        [recognizer setScale:1.0];
    }

    if (state == UIGestureRecognizerStateEnded ) {
        NSLog(@"%d",recognizer.view.bounds.size.width < _vFrame.size.width);
        //recognizer.view.frame = _vFrame;
    }
}
-(void)rotationGestureDetected:(UIRotationGestureRecognizer*)recognizer{
    //旋转
    UIGestureRecognizerState state = [recognizer state];
    if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged) {
        CGFloat rotation = [recognizer rotation];
        [recognizer.view setTransform:CGAffineTransformRotate(recognizer.view.transform, rotation)];
        [recognizer setRotation:0];
    }
}
-(void)panGestureDelegated:(UIPanGestureRecognizer*)recognizer{
    //移动
    UIGestureRecognizerState state = [recognizer state];
    if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged) {
        CGPoint translation = [recognizer translationInView:recognizer.view];
        [recognizer.view setTransform:CGAffineTransformTranslate(recognizer.view.transform, translation.x, translation.y)];
        [recognizer setTranslation:CGPointZero inView:recognizer.view];
    }
}
-(void)tapGestureDelegated:(UITapGestureRecognizer*)tapGesture{
    //双击
    NSLog(@"双击");
    
}
-(void)tapOneGestureDelegated:(UITapGestureRecognizer*)tapGesture{
    NSLog(@"单击");
    [tapGesture.view removeFromSuperview];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
